import 'package:dio/dio.dart';
import 'package:flutter_application_1/http/base_model.dart';

class ResPostIntercepter extends Interceptor {
  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    if (response.statusCode == 200) {
      try {
        // errorCode = 0 代表执行成功，不建议依赖任何非0的 errorCode.
        // errorCode = -1001 代表登录失效，需要重新登录。
        var rsp = BaseModel.fromJson(response.data);
        if (rsp.errorCode == 0) {
          if(rsp.data == null) {
            handler.next(Response(requestOptions: response.requestOptions, data: true));
          }else {
            handler.next(Response(requestOptions: response.requestOptions, data: rsp.data));
          }
        } else if(rsp.errorCode == -1001){
          handler.reject(DioException(requestOptions: response.requestOptions, message: "登录失效，请重新登录"));
        } else {
          handler.reject(DioException(requestOptions: response.requestOptions, message: rsp.errorMsg));
       }
      } catch (e) {
        handler.reject(DioException(requestOptions: response.requestOptions, message: e.toString()));
      }
    } else {
      handler.reject(DioException(requestOptions: response.requestOptions, message: "网络错误"));
    }
  }
}